package vn.edu.hut.soict.translate;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import vn.edu.hut.soict.translate.dictionary.pos.POSDictionary;
import vn.edu.hut.soict.translate.dictionary.pos.POSDictionaryReader;
import vn.edu.hut.soict.translate.wordgraph.SimpleWord;
import vn.edu.hut.soict.translate.wordgraph.Word;
import vn.hus.nlp.tokenizer.AutomataTokenizer;
import vn.hus.nlp.tokenizer.DefaultTokenizerFactory;
import vn.hus.nlp.tokenizer.Token;

public class SimpleSentenceSplitter implements SentenceSplitter {
	private AutomataTokenizer tokenizer;
	private POSDictionary dictionary;
	private DefaultTokenizerFactory tokenizerFactory;
	
	public SimpleSentenceSplitter() {
		POSDictionaryReader reader = new POSDictionaryReader();
		dictionary = reader.read(POSDictionary.DICTIONARY_FILE);
		try {
			tokenizerFactory = new DefaultTokenizerFactory("res");
		}
		catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Override
	public Word[] split(String s) {
		tokenizer = (AutomataTokenizer) tokenizerFactory.getTokenizer(s);
		List<Word> words = new ArrayList<Word>();
		while (tokenizer.hasNext()) {
			Token token = tokenizer.next();
			String text = token.getText();
			List<String> partOfSpeechs = dictionary.partOfSpeechs(text);
			words.add(new SimpleWord(text, partOfSpeechs));
		}
		return words.toArray(new Word[words.size()]);
	}
}
